home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 103 / CD-ROM 103.iso / edu / martianwin / src / gameover.c < prev    next >
Encoding:
Text File  |  2003-08-11  |  3.4 KB  |  137 lines

  1. void hiscore_events()
  2. {
  3.     int n;
  4.     SDL_Event event;
  5.     
  6.     while(SDL_PollEvent(&event))
  7.     {
  8.         if(event.type==SDL_KEYDOWN)
  9.         {
  10.         if(event.key.keysym.sym>41)
  11.         {
  12.             playername[namechar] = event.key.keysym.sym;
  13.             if(playername[namechar]>=97 && playername[namechar]<=122)
  14.                 playername[namechar]=event.key.keysym.sym-32;
  15.         if(namechar < 9)
  16.             namechar++;
  17.         }
  18.         else
  19.         {
  20.             if(event.key.keysym.sym==SDLK_RETURN || event.key.keysym.sym == SDLK_ESCAPE)
  21.                 inputloop=0;
  22.         if(event.key.keysym.sym==SDLK_BACKSPACE)
  23.         {
  24.             namechar=0;
  25.             for(n=0;n<10;n++)
  26.                 playername[n] = '.';
  27.         }
  28.         }
  29.     }
  30.     }
  31. }
  32.  
  33. void show_hiscores()
  34. {
  35.     SDL_SetColorKey(scorefont2,SDL_SRCCOLORKEY,SDL_MapRGB(scorefont2->format,0,255,0));
  36.     char plyername[]="          ";
  37.     T_Print(scorefont2,20,25,"   MARTIAN MEMORY     TOP TEN    ");
  38.     T_Print(scorefont2,20,55,"NAME           TIME        SCORE ");
  39.     T_Print(scorefont2,20,75,"---------------------------------");
  40.  
  41.     T_Print(scorefont2,20,400,"---------------------------------");
  42.     T_Print(scorefont2,20,425,"    PRESS RIGHT MOUSE BUTTON     ");
  43.     T_Print(scorefont2,20,450,"            TO RETURN            ");
  44.  
  45.     for(int a=0; a < 10;a++)
  46.     {
  47.     strncpy(playername,scorename[a],10);
  48.     T_Print(scorefont2,5,100+(25*a),"%s",playername);
  49.     int mins=int(scoretime[a]/60);
  50.     int secs=int(scoretime[a]-(mins*60));
  51.     if(secs>9) T_Print(scorefont2,290,100+(25*a),"%d:%d",mins,secs);
  52.     else T_Print(scorefont2,290,100+(25*a),"%d:0%d",mins,secs);
  53.     T_Print(scorefont2,510,100+(25*a),"%d",scorescore[a]);
  54.     }
  55.     SDL_Flip(screen);
  56. }
  57.  
  58. void do_hiscores()
  59. {
  60.     flush_events();
  61.     load_hiscoredata();
  62.     //--- creamos el fondo de pantalla
  63.     int n;
  64.     float R, G, B;
  65.     R=50; G=130; B=180;
  66.     SDL_Rect back;
  67.     back.h=1; back.w=screen_w; back.x=0;
  68.     for(n=0; n<screen_h;n++)
  69.     {
  70.     back.y=n;
  71.     SDL_FillRect(screen, &back, SDL_MapRGB(screen->format,int(R), int(G), int(B)));
  72.     R-=0.33; G-=0.25; B-=0.50;
  73.     if(R<0) R=0; if(G<0) G=0; if(B<0) B=0;
  74.     }
  75.     
  76.     show_hiscores();    
  77.     wait_mouse();
  78.     unload_hiscoredata();
  79. }
  80.  
  81. void do_gameover()
  82. {
  83.     load_hiscoredata();
  84.     char nameplayer[]="          ";
  85.     strncpy(playername,nameplayer,10);
  86.     namechar=0;
  87.     int highscore=0;
  88.     for(int a=9; a>=0;a--)
  89.     {
  90.     if(score>scorescore[a])
  91.     {
  92.         highscore=1;
  93.         scorepos=a;
  94.     }
  95.     if(score==scorescore[a]) // comprobamos que tiempo fue mejor
  96.         if(gametime<scoretime[a]) 
  97.         {
  98.         highscore=1;
  99.         scorepos=a;
  100.         }
  101.     }
  102.     if(highscore==1) // si se hizo una buena puntuacion
  103.     {
  104.     for(int b=8;b>=scorepos;b--)
  105.     {
  106.         strncpy(scorename[b+1],scorename[b],10);
  107.         scorescore[b+1]=scorescore[b];
  108.         scoretime [b+1]=scoretime [b];
  109.     }
  110.     inputloop=1;
  111.     SDL_Rect box;
  112.     box.x=175;box.y=220;box.w=290;box.h=52;
  113.     SDL_FillRect(screen,&box,SDL_MapRGB(screen->format,255,255,255));
  114.     box.x+=3;box.y+=3;box.w-=6;box.h-=6;
  115.     while(inputloop==1)
  116.     {
  117.         hiscore_events();
  118.         SDL_FillRect(screen,&box,SDL_MapRGB(screen->format,0,0,0));
  119.         T_Print(scorefont2,box.x+10,box.y+5,"ENTER YOUR NAME");
  120.         T_Print(scorefont2,box.x+51+(namechar*16),box.y+29,".");
  121.         T_Print(scorefont2,box.x+51,box.y+24,"%s",playername);
  122.         SDL_Flip(screen);
  123.     }
  124.     strncpy(scorename[scorepos],playername,10);
  125.     scorescore[scorepos]=score;
  126.     scoretime[scorepos]=int(gametime);
  127.     save_hiscoredata();
  128.     }
  129.     unload_hiscoredata();
  130. }
  131.  
  132. void game_over()
  133. {
  134.     do_gameover();
  135.     do_hiscores();
  136. }
  137.